import { loadBase64 } from "../util.js";
import { Texture, BaseTexture , Sprite, Container } from 'pixi.js'


export class Numbers {
    constructor(resource) {
        this.root = resource.Content.Effect['BasicEff.img'];
        this.performance = {
            NoBlue: {},
            NoCri: {},
            NoRed: {},
            NoViolet: {},
        }
    }
    async initPerformance() {
        for (let key in this.performance) {
            const Numbers = this.root[`${key}0`];
            for (let name of Object.keys(Numbers)) {
                let image = await loadBase64(Numbers[name].value);
                let base = new BaseTexture(image);
                let texture = new Texture(base);
                // texture.defaultAnchor.x = (Numbers[name].origin.x) / image.width;
                // texture.defaultAnchor.y = (Numbers[name].origin.y) / image.height;
                this.performance[key][name] = texture;
            }
        }
    }
    async init() {
        await this.initPerformance();
    }

    getSprite(num, type) {
        let sprite = new Container();
        const array = [];
        let length = 0;
        let n = num;
        while (n) {
            array.push(n % 10);
            n = Math.floor(n / 10);
            length ++;
        }
        let w = 0;
        while (array.length !== 0) {
            let sitNum = array.pop();
            let sp = new Sprite(this.performance[type][sitNum]);
            sp.x = w;
            sprite.addChild(sp);
            w += sp.width;
        }
        sprite.zIndex = 100000;
        return sprite;
    }
}


